<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../../../layuiadmin/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../../layuiadmin/style/admin.css" media="all">
		<link rel="stylesheet" href="../../../../css/common.css" media="all">

	</head>

	<body>

		<div class="layui-fluid">
			<div class="layui-row" id="view">
				<form class="layui-form" lay-filter="test_detail">
					<div class="layui-card">
						<div class="layui-card-body">
							<div class="layui-form-item">
								<div class="layui-inline">
									<label class="layui-form-label">用户编号</label>
									<div class="layui-input-inline">
										<input type="text" name="id" placeholder="请输入用户编号" autocomplete="off" class="layui-input" readonly >
									</div>
								</div>
								<div class="layui-inline">
									<label class="layui-form-label">会员编号</label>
									<div class="layui-input-inline">
										<input type="text" name="memberid" placeholder="请输入姓名" autocomplete="off" class="layui-input" readonly>
									</div>
								</div>
							</div> 
							<div class="layui-form-item">
								<div class="layui-inline">
									<label class="layui-form-label">用户名称</label>
									<div class="layui-input-inline">
										<input type="text" name="name" placeholder="请输入用户名称" autocomplete="off" class="layui-input">
									</div>
								</div>
								<div class="layui-inline">
									<label class="layui-form-label">性别</label>
									<div class="layui-input-inline sex"></div>
								</div>
							</div> 
							<!--<div class="layui-form-item">
								<div class="layui-inline">
									<label class="layui-form-label">登录帐号</label>
									<div class="layui-input-inline">
										<input type="text" name="account" placeholder="请输入登录帐号" autocomplete="off" class="layui-input">
									</div>
								</div>
							</div>  -->
							
  							<div class="layui-form-item">
								<div class="layui-inline">
									<label class="layui-form-label">用户生日</label>
									<div class="layui-input-inline">
										<input type="text" name="birthday" id="birthday" placeholder="请选择用户生日" autocomplete="off" class="layui-input">
									</div>
								</div> 
							</div>
							<div class="layui-form-item">
								<div class="layui-inline">
									<label class="layui-form-label">手机号码</label>
									<div class="layui-input-inline">
										<input type="text" name="mobile" placeholder="请选择手机号码" autocomplete="off" class="layui-input">
									</div>
								</div>
								<div class="layui-inline">
									<label class="layui-form-label">邮箱</label>
									<div class="layui-input-inline">
										<input type="text" name="email" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
									</div>
								</div>
							</div>
							<!--<div class="layui-form-item">
								<div class="layui-block">
									<label class="layui-form-label">会员折扣<span style="color:red">*</span></label>
									<div class="layui-input-block">
										<input type="text" name="discount" placeholder="请输入折扣：99 代表 商品价*0.99" lay-verify="discount"  autocomplete="off" class="layui-input">
									</div>
								</div> 
							</div>-->
							<!-- <div class="layui-form-item">
								<div class="layui-block">
									<label class="layui-form-label">密码</label>
									<div class="layui-input-block"> 
										<input type="password" name="code" autocomplete="new-password" placeholder="请输入新的密码" class="layui-input" hidden>
									</div>
								</div>
							</div> 
							<div class="layui-form-item">
								<div class="layui-block">
									<label class="layui-form-label">支付密码</label>
									<div class="layui-input-block"> 
										<input type="password" name="paycode" autocomplete="new-password" placeholder="请输入新的支付密码" class="layui-input" hidden>
									</div>
								</div>
							</div> -->
						</div>
					</div> 

					<div class="layui-form-item layui-layout-admin">
						<div class="layui-input-block">
							<div class="layui-footer" style="left: 0;"> 
								<button class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>
		<script src="../../../../layuiadmin/layui/layui.js"></script>
		<script src="../../../../js/jquery.min.js"></script>
		<script src="https://api.lishuahuoban.cn/layuiadmin/city.data.js"></script>
		<script src="../../../../js/common.js"></script>
		<script>
			var _data = {
				id: getParam('id')
			}
			layui.config({
				base: '../../../../layuiadmin/' //静态资源所在路径
			}).extend({
				index: 'lib/index' //主入口模块
			}).use(['index', 'form', 'laytpl', 'laydate'], function() {
				var $ = layui.$,
					admin = layui.admin,
					laytpl = layui.laytpl,
					laydate = layui.laydate,
					form = layui.form;
				 //常规用法
				  laydate.render({
				    elem: '#birthday'
				  });
				var area_cn_arr = [],
					img_arr2 = [];
				
				sex_show('radio');
				initdata();
				//监听提交
				form.on('submit(demo1)', function(data) {
					var data = data.field;
					_data = Object.assign(_data, data);
					_data.card_status = 0;
					_data.area_cn = area_cn_arr.join(',')
					layer.confirm('确定提交数据？', function() {
						loading();
						submitdata();
					}, function() {
						layer.msg('取消操作');
					});
					return false;
				});

				function submitdata() {
					$.post(server.sys_user_edit, _data, function(res, status) {
						if(status == 'success') {
							if(res.code == 0) {
								layer.alert('操作成功', function() {
									parent.layer.closeAll();
									parent.layui.table.reload('test-table-operate');
								});
								layer.closeAll('loading');
							} else {
								layer.msg(res.msg);
								layer.closeAll('loading');
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}

				window.choose = function(data) {
					layer.open({
						type: 2,
						title: '上传图片',
						shadeClose: false,
						shade: 0.5,
						maxmin: true, //开启最大化最小化按钮
						area: ['700px', '400px'],
						content: '../../model/up_report_one.html?type=' + data,
						end: function() {},
						success: function() {}
					});
				}

				window.getimg = function(data, type) {
					console.log(data);
					if(data) {
						switch(type) {
							case '1':
								createli(data, 'front', type)
								break;
							case '2':
								createli(data, 'back', type)
								break;
							default:
								break;
						}
					}
				}
				window.delimg = function(i, type, _self) {
					var _type = String(type)
					layer.confirm('确定要删除图片？', function(index) {
						switch(_type) {
							case '1':
								img_arr1.splice(i, 1)
								break;
							case '2':
								img_arr2.splice(i, 1)
								break;
							default:
								break;
						}
						$(_self).parent().remove();
						layer.close(index)
					}, function() {
						layer.msg('取消操作');
					});
				}

				function createli(data, dom, type) {
					var lihtml = '';
					switch(type) {
						case '1':
							if(data.indexOf(',') == -1) {
								img_arr1.splice(0, 1, data)
							} else {
								for(var i = 0; i < data.split(',').length; i++) {
									img_arr1.push(data.split(',')[i])
								}
							}
							for(var i = 0; i < img_arr1.length; i++) {
								lihtml += '<li class="show_img_li1">' +
									'<img src=' + img_arr1[i] + ' >' +
									// '<p onclick="delimg(' + i + ',1,this)">删除</p>' +
									'</li>'
							}
							break;
						case '2':
							if(data.indexOf(',') == -1) {
								img_arr2.splice(0, 1, data)
							} else {
								for(var i = 0; i < data.split(',').length; i++) {
									img_arr2.push(data.split(',')[i])
								}
							}
							for(var i = 0; i < img_arr2.length; i++) {
								lihtml += '<li class="show_img_li1">' +
									'<img src=' + img_arr2[i] + ' >' +
									// '<p onclick="delimg(' + i + ',2,this)">删除</p>' +
									'</li>'
							}
							break;
						default:
							break;
					}
					$('#' + dom).html(lihtml);
				}

				function initdata() {
					$.get(server.sys_user_detail, _data, function(res, status) {
						if(status == 'success') {
							if(res.code == 0) {
								var data = res.px_system_user

								$(".sex radio[value=" + data.data.sex + "]").attr("checked", true)
								$(".member_type radio[value=" + data.data.value + "]").attr("checked", true)
								$(".agent_type input[value=" + data.data.type + "]").attr("checked", true)

								data.data.front ? createli(data.data.front, 'front', '1') : ''
								data.data.back ? createli(data.data.back, 'back', '2') : ''

								if($("select[name=area_cn] option").length > 0) {
									$("select[name=area_cn]").val(data.data.area_id);
								}
								if(data.data.area_id) {
									queryareadata1(data.data.area_id, 'area_id')
								}

								form.val("test_detail", data.data);
								var unitType = [];
								unitType = data.data.type.split(",");
								var unitTypeCheckbox = $(".member_type").children('input');
								
								for (var j = 0; j < unitType.length; j++) {
									for (var i = 0; i < unitTypeCheckbox.length; i++) {
										if (unitTypeCheckbox[i].value == unitType[j]) {
											unitTypeCheckbox[i].checked = true;
										}
									}
								}
								form.render();
							} else {
								layer.msg(res.msg);
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}
				
				var htmlSelect = function(optionData, selectname) {
					var html = '';
					html += '<div class="layui-input-inline">';
					html += ' <select lay-filter=' + selectname + ' name=' + selectname + '>';
					html += '  <option value="">请选择</option>';
					for(var i = 0; i < optionData.length; i++) {
						html += ' <option value=' + optionData[i].id + ' title=' + optionData[i].id + '>' + optionData[i].name +
							'</option>';
					}
					html += ' </select>';
					html += '</div>';
					return html;
				}
					/*--地区开始--*/
				function queryareadata1(fid) {
					loading();
					area1 = [];
					if(fid) {
						$.ajax({
							url: server.queryarea,
							data: {
								id: fid
							},
							type: 'get',
							success: function(res) {
								if(res.code == 0) {
									var data = res.area_list.data;

									area1.unshift(data[0])
									var str = data[0].sid
									var sid = str.substr(0, str.lastIndexOf('.', str.lastIndexOf('.') - 1) + 1);

									queryarealdata2(sid)
								}
							},
							error: function(res) {
								errorshow();
							}
						})
					}
				}

				function queryarealdata2(sid) {
					if(sid) {
						$.ajax({
							url: server.queryarea,
							data: {
								sid: sid
							},
							type: 'get',
							success: function(res) {
								if(res.code == 0) {
									var data = res.area_list.data;

									if(area1[0].id != data[0].id) {
										area1.unshift(data[0])
									}
									if(data[0].fid != 20980) {
										var sid = data[0].sid.substr(0, data[0].sid.lastIndexOf('.', data[0].sid.lastIndexOf('.') - 1) + 1);
										queryarealdata2(sid)
									} else {
										var selected1 = [];
										for(var i = 0; i < area1.length; i++) {
											selected1.push(area1[i].id)
										}
										initarea('area_id', selected1);

										closeloading()
									}
								}
							},
							error: function(res) {
								closeloading();
								errorshow();
							}
						})
					}
				}

				var initareadata = function(fid) {
					var data1;
					if(fid) {
						$.ajax({
							url: server.queryarea,
							data: {
								fid: fid
							},
							async: false,
							type: 'get',
							success: function(res) {
								if(res.code == 0) {
									var data = res.area_list.data;
									data1 = data;
								}
							},
							error: function(res) {
								errorshow();
							}
						})
					}
					return data1;
				}

				var area_item = initareadata(20980);

				var initarea = function(domname, selected) {
					var selected = selected;
					var $selectWrap = $('#' + domname);
					$selectWrap.find('.layui-input-inline').remove();
					var html = htmlSelect(area_item, domname);
					$selectWrap.append(html);
					if(selected) {
						var index = [];
						for(var i = 0; i < selected.length; i++) {
							var selectlist = $selectWrap.find('select>option')

							for(var j = 0; j < selectlist.length; j++) {
								var title = $(selectlist[j]).attr('title');
								if(title == selected[i]) {
									$(selectlist[j]).attr('selected', true);
								}
							}
							// 取出下级的选项值
							if(i != selected.length - 1) {
								var childItem = initareadata(selected[i]);
								// 下级选项值存在则创建select
								if(childItem) {
									var html = htmlSelect(childItem, domname);
									$selectWrap.append(html);
								}
							}
						}
					}
					form.render('select');
					// 监听select
					form.on('select(' + domname + ')', function(data) {
						var $thisItem = $(data.elem).parent();
						$thisItem.nextAll('div.layui-input-inline').remove();

						var fid = data.elem[data.elem.selectedIndex].title;
						var cat_cn = data.value
						
						switch(domname) {
							case 'area_id':
								_data.cat_id = fid
								break;
							default:
								break;
						}

						var childItem = initareadata(fid);

						if(childItem.length > 0) {
							var html = htmlSelect(childItem, domname);
							$selectWrap.append(html);
							$selectWrap.find('select:last-child').attr('lay-verify', '');
							form.render('select');
						}
					});
				}
				initarea('area_id');
				/*--地区结束--*/

			});
		</script>
	</body>

</html>